import {
    queryList,
    addData,
    updateData,
    deleteData,
    queryData,
    updateStatus,
  } from './service';

  import {getAuthority} from '@/utils/authority';
  import { message } from 'antd';
  
  const Model = {
    namespace: 'userModel',    /*定义model的名称,表示在全局 state 上的 key不能重复 */
    /* model的状态属性 */
    state: {
      dataSource: [],
      total: 0,
      queryParams: {
        start: 0,
        count: 0,
        query: {
            productId: undefined,
            deviceName: undefined,
            status: undefined,
            newVersion: undefined,
        }
      }
    },
    /*定义model请求service事件,对外的事件 */
    effects: {
      /*list 变量名 { payload: { params } } 参数,{ call, put }注进来的工具 call调用外部方法 put调用内部方法 */
      *list({ payload: { params } }, { call, put }) {
        const reqParams = {
          start: params.start,
          count: params.count,
          query: JSON.stringify(params.query)
        }
        /* 用call调用service方法 */
        const response = yield call(queryList, reqParams);
        const success = response.success;
 
        if (success) {
          /*返回结果通过put方法调用reducer中的方法来修改state */
          yield put({
            type: 'reducerQueryList',
            payload: {
              rows: response.data.rows,
              total: response.data.total,
              queryParams: params
            },
          });
        } else {
          message.warning(response.data)
        }
      },
      *modelList({ payload: { params },callback }, { call, put }) {
        const reqParams = {
          start: params.start,
          count: params.count,
          query: JSON.stringify(params.query)
        }
        /* 用call调用service方法 */
        const response = yield call(queryList, reqParams);
        const success = response.success;
        if (success) {
           if(callback){
             callback(response.data);
           }
        } else {
          message.warning(response.data)
        }
      },
      *get({ id,callback }, { call }) {      
        const reps = yield call(queryData, id);
        const success = reps.success;
        if (success) {
          if (callback) callback(reps.data);
        } else {
          message.warning(reps.errorMessage)
        }
      },
      *delete({ id }, { call, put, select }) {
        const params = yield select(state => state.userModel.queryParams);
        const response = yield call(deleteData, id);
        const success = response.success;
        if (success) {
          message.success('删除成功!');
          /*通过调用 effects 中的 list 方法来重新获取数据并修改state状态刷新界面*/
          yield put({
            type: 'list',
            payload: {
              params
            }
          });
        } else {
          message.warning(response.errorMessage)
        }
      },
      *status({ id,status }, { call, put, select }) {
        const params = yield select(state => state.userModel.queryParams);
        const response = yield call(updateStatus, id,status);
        const success = response.success;
        if (success) {
          message.success('修改成功!');
          /*通过调用 effects 中的 list 方法来重新获取数据并修改state状态刷新界面*/
          yield put({
            type: 'list',
            payload: {
              params
            }
          });
        } else {
          message.warning(response.data)
        }
      },
      *submit({ payload, callback }, { call, put, select }) {
        let dataRequest;
        let alertContent = '';
        if (payload.productId) {
          alertContent = '修改成功!';
          dataRequest = updateData;
        } else {
          alertContent = '添加成功!';
          dataRequest = addData;
        }
        const params = yield select(state => state.productModel.queryParams);
        const response = yield call(dataRequest, payload);
        const success = response.success;
        if (callback) callback(response);
        if (success) {
          message.success(alertContent);
          
          yield put({
            type: 'list',
            payload: {
              params
            }
          });
        } else {
          message.warning(response.data)
        }
      },
    },
    /*用于修改state状态的事件,或者合并state */
    reducers: {
      reducerQueryList(state, { payload: { rows, total, queryParams } }) {
 
        /*这是一种特殊的语法，给state属性赋值 */
        return { ...state, dataSource: rows, total, queryParams };
      },
    },
  };
  export default Model;
  